package com.gua.jbs.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.gua.jbs.dao.IStockDao;
import com.gua.jbs.pojo.Jbspst;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.ExceptionMessage;
import com.gua.jbs.utility.QueryConstant;

public class StockDao extends BaseDao implements IStockDao {
	private static final Logger logger = Logger.getLogger(StockDao.class);

	public boolean addStock(Jbspst jbspst) throws ApplicationException {
		List lParamList = new ArrayList();
		boolean isSaved = false;
		int pk = 0;
		if (jbspst != null) {
			// set the parameters

			try {
				pk = getPrimaryKey("PROD_STOCK_ID", "JBSPST");
			} catch (Exception e) {

			}
			lParamList.add(0, pk);
			lParamList.add(1, jbspst.getProdId());
			lParamList.add(2, jbspst.getPrevStock());
			lParamList.add(3, jbspst.getCurrentStock());
			lParamList.add(4, jbspst.getIsAdded());
			lParamList.add(5, jbspst.getIsActive());
			lParamList.add(6, new Timestamp(new Date().getTime()));
			lParamList.add(7, new Timestamp(new Date().getTime()));
			lParamList.add(8, null);
			lParamList.add(9, null);
			lParamList.add(10, null);
			lParamList.add(11, "Piyush");
			lParamList.add(12, "Piyush");

			// Get the query by passing parameters
			String query = getQuery(QueryConstant.SAVE_PRODUCT_STOCK,
					lParamList);
			int resultCount = 0;
			try {
				resultCount = insert(query);
				if (resultCount != 0)
					isSaved = true;
			} catch (SQLException sqe) {
				logger.error("StockDao : addStock() method --->"
						+ sqe.getMessage());
				throw new ApplicationException(
						ExceptionMessage.PRODUCT_STOCK_NOT_ADDED_OR_UPDATED);
			}
		}
		return isSaved;
	}

	/**
	 * Method to get the current stock of a product
	 * 
	 * @return
	 * @throws ApplicationException
	 */
	public int getStockByProductId(String productId)
			throws ApplicationException {
		List lParamList = new ArrayList();
		// Get the query by passing parameters
		lParamList.add(0, productId);
		lParamList.add(1, DBConstant.IS_ACTIVE_YES);

		String query = getQuery(QueryConstant.GET_PRODUCT_LATEST_STOCK,
				lParamList);
		ResultSet rs = null;
		int prevStock = 0;
		int currentStock = 0;
		int totalStock = 0;
		try {
			rs = getResults(query);
			if (rs != null) {
				while (rs.next()) {
					prevStock = rs.getInt("PREV_STOCK");
					currentStock = rs.getInt("CURR_STOCK");

					// Total stock will be the sum of previous and current stock
					totalStock = prevStock + currentStock;
					break;
				}
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.STOCK_NOT_RETRIEVED);
		}
		return totalStock;
	}

	/**
	 * Method to return the stock details of all products in system
	 */
	public List<Object[]> getAllProductStock() throws ApplicationException {
		List<Object[]> productStockList = null;
		List lParamList = new ArrayList();
		// Get the query by passing parameters
		lParamList.add(0, DBConstant.IS_ACTIVE_YES);
		String query = getQuery(QueryConstant.GET_PRODUCTS_STOCK, lParamList);
		ResultSet rs = null;
		try {
			rs = getResults(query);
			if (rs != null) {
				productStockList = new ArrayList<Object[]>();
				while (rs.next()) {
					Object[] stockDetails = new Object[4];
					int prevStock = rs.getInt("PREV_STOCK");
					int currentStock = rs.getInt("CURR_STOCK");
					// Total stock will be the sum of previous and current stock
					int totalStock = prevStock + currentStock;

					String productId = rs.getString("PROD_ID");
					String productName = rs.getString("PROD_NAME");
					String productCode = rs.getString("PROD_CODE");
					stockDetails[0] = productId;
					stockDetails[1] = productName;
					stockDetails[2] = productCode;
					stockDetails[3] = "" + totalStock;
					productStockList.add(stockDetails);
				}
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.STOCK_NOT_RETRIEVED);
		}
		return productStockList;
	}

}
